package com.four.live.controller;


import com.four.live.fegin.ShopFeginService;
import com.four.live.service.ILiveService;
import com.guli.common.utils.R;
import com.guli.common.vo.ManageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.four.live.entity.Live;
import com.four.live.service.ILiveService;
import com.four.live.vo.LiveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gs
 * @since 2021-12-23
 */
@RestController
@RequestMapping("live/live")
public class LiveController {

    @Autowired
    private ILiveService liveService;

    @Autowired
    private ShopFeginService shopFeginService;

    /**
     * 查询所有商品
     * @return
     */
    @GetMapping("/findManage")
    public R getManage(){
        List<ManageVo> manageVoList=shopFeginService.findAllManage();
        return R.ok().put("manageVoList",manageVoList);
    }

    @GetMapping("/findManageById/{manId}")
    public R findManageById(@PathVariable("manId")Integer manId) {
        ManageVo manageVo = shopFeginService.getManageById(manId);
        return R.ok().put("manageVo", manageVo);
    }

    /**
     * 直播列表，精准，分页，模糊
     *
     * @param vo
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/list")
    public IPage<Live> list (@RequestBody LiveVo vo,
                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "3") Integer size){

        return liveService.liveList(vo, page, size);
    }

    /**
     * 直播视频根据状态删除
     *
     * @param id
     */
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable Long id){

        Live deleteById = liveService.findById(id);
        System.out.println(deleteById);
        if (deleteById.getLiveStatus()==0){
            liveService.deleteById(id);
        }
    }

    /**
     *直播根据直播开启的状态进行详情
     *
     * @param id
     */
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Long id){

        Live deleteById = liveService.findById(id);
        System.out.println(deleteById);
        if (deleteById.getLiveStatus()==0){
            Live live=   liveService.selectList(id);
            return R.ok().put("list",live);
        }
    return R.error();
    }

    /**
     * 创建直播
     * @param live
     * @return
     */
    @PostMapping("/addLive/{loginName}")
    public R addLive(@RequestBody Live live,@PathVariable("loginName")String loginName){
        liveService.addLive(live,loginName);
        return R.ok();
    }

    /**
     * 开启关闭直播
     * @param liveId
     * @return
     */
    @PutMapping("/openLive/{liveId}")
    public R openLive(@PathVariable("liveId")Integer liveId){
        liveService.openLive(liveId);
        return R.ok();
    }
}
